import request from "../../../../../utils/request";

/**
 @ClassName:pageMixins
 @Author:TwT
 @Date:2022/6/19 0:45
 @Version:1.0
 **/
export default {
    props: {
        /*表单项*/
        formDesc: {
            type: Object,
            required: true
        },
        /*表单校验规则*/
        rules: [Object],
        /*主键属性*/
        idProp: {
            type: String,
            default: "id"
        },
        /* 分页请求url */
        urlPage: {
            type: String,
            required: true
        },
        /* 添加请求url */
        urlAdd: {
            type: String,
            required: true
        },
        /* 编辑请求url */
        urlUpdate: {
            type: String,
            required: true
        },
        /*查询单个url*/
        urlQueryById: {
            type: String,
            required: true
        },
        /*删除请求*/
        urlDeleteByIds: {
            type: String,
            required: true
        },
    },
    computed: {
        //表格列
        columns() {
            if (!this.formDesc) return []
            let cols = []
            Object.keys(this.formDesc).forEach(key => {
                const colCfg = this.formDesc[key].colCfg
                //不显示
                if (colCfg && colCfg.hidden) return;

                if (!colCfg) cols.push({field: key, title: this.formDesc[key].label})
                if (colCfg && !colCfg.hidden) {
                    let tmp = {
                        field: colCfg.field || key,
                        title: colCfg.label || this.formDesc[key].label,
                        colCfg
                    }
                    cols.push(tmp)
                }
            })
            return cols
        },
        //表单项
        formItems() {
            if (!this.formDesc) return []

            let formItems = {}
            Object.keys(this.formDesc).forEach(key => {
                if (!this.formDesc[key].hidden) formItems[key] = this.formDesc[key]
            })
            return formItems
        }
    },
    data() {
        return {
            /* 表格data */
            tableData: [],
            selectRows: [],
            /* 抽屉 */
            isAddDrawer: false,
            isUpdateDrawer: false,
            isDetailDrawer: false,
        };
    },
    methods: {
        loadTableData() {
            this.$nextTick(() => {
                this.$refs['pagination'].loadTableData()
            })
        },
        selectChangeEvent({records}) {
            this.selectRows = records
        },
        onDelete() {
            const deleteIds = this.selectRows.map(item => {
                return item[this.idProp]
            }).join(",")
            if (!this.urlDeleteByIds) return
            request({
                url: `${this.urlDeleteByIds}`,
                data: {ids: deleteIds},
                method: 'delete',
            }).then(data => {
                this.$message({
                    message: '删除成功',
                    type: 'success'
                });
                this.loadTableData()
            })
        },
        /* 新增抽屉 */
        openAddDrawer() {
            this.isAddDrawer = true
        },
        saveSuccess() {
            this.isAddDrawer = false
            this.loadTableData()
        },
        /* 修改抽屉 */
        openUpdateDrawer() {
            this.isUpdateDrawer = true
        },
        updateSuccess() {
            this.isUpdateDrawer = false
            this.loadTableData()
        },
        /* 详情抽屉 */
        openDetailDrawer() {
            this.isDetailDrawer = true
        },
    }
}